<template>
  <div id="admin-manage" class="page-wrapper">
    <div class="selector-wrapper main-card wrapper">
      <div class="main-card">
        <el-form class="form" :inline="true" ref="form" :model="formData">
          <el-form-item label="订单编号" prop="orderId">
            <el-input
              v-model.trim="formData.orderId"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="姓名" prop="userName">
            <el-input
              v-model.trim="formData.userName"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="电话" prop="userPhone">
            <el-input
              v-model.trim="formData.userPhone"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="购买时间">
            <el-date-picker
              v-model="formData.time"
              type="datetimerange"
              :picker-options="pickerOptions"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              align="right"
            ></el-date-picker>
          </el-form-item>
        </el-form>
      </div>
      <el-button type="primary" @click="handleSubmitClick">获取数据</el-button>
    </div>
    <div class="wrapper main-card">
      <el-table
        :data="pageData"
        style="width: 100%"
        :stripe="true"
        @selection-change="handleSelectionChange"
        row-key="id"
      >
        <el-table-column prop="orderId" label="订单号"></el-table-column>
        <el-table-column prop="userName" label="姓名"></el-table-column>
        <el-table-column prop="userPhone" label="电话号"></el-table-column>
        <el-table-column prop="goodsName" label="商品名称"></el-table-column>
        <el-table-column prop="goodsNumber" label="购买数量"></el-table-column>
        <el-table-column prop="totalMoney" label="应付金额"></el-table-column>
        <el-table-column prop="userAddress" label="收货地址"></el-table-column>
        <el-table-column
          prop="trackingNumber"
          label="快递单号"
        ></el-table-column>
        <el-table-column label="付款状态">
          <template slot-scope="scope">
            <span>
              {{
                scope.row.paystate === 0
                  ? '未付款'
                  : scope.row.paystate === 1
                  ? '已付款'
                  : scope.row.paystate === 2
                  ? '待退款'
                  : scope.row.paystate === 3
                  ? '已退款'
                  : '未知状态'
              }}
            </span>
          </template>
        </el-table-column>

        <el-table-column label="订单状态">
          <template slot-scope="scope">
            <span>
              {{
                scope.row.state === '0'
                  ? '待付款'
                  : scope.row.state === '1'
                  ? '待发货'
                  : scope.row.state === '2'
                  ? '待收货'
                  : scope.row.state === '3'
                  ? '已完成'
                  : '未知状态'
              }}
            </span>
          </template>
        </el-table-column>
        <el-table-column type="selection" width="55" />
      </el-table>

      <el-dialog title="确认发货" :visible.sync="dialogVisible" width="25%">
        <div class="dialog-style">
          <span style="margin-right:10px;">填写快递单号:</span>
          <div>
            <el-input
           
              v-model="modifydata.trackingNumber"
            ></el-input>
         
          </div>
        </div>   <span slot="footer" class="dialog-footer">
              <el-button @click="dialogVisible = false">取消</el-button>
              <el-button type="primary" @click="confirmReceiveOrder"
                >确认发货</el-button
              >
            </span>
      </el-dialog>

      <div class="button-wrapper">
        <el-button @click="handleReceiveOrder" type="primary"
          >一键发货</el-button
        >
      </div>

      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { searchsendorderlist } from '@/api/pcclinet'
import { allsend } from '@/api/pcclinet'
export default {
  name: 'RoomInfo',
  data() {
    return {
      currentPage: 1,
      pageData: [],
      total: '',
      pageSize: 10,
      dialogVisible: false,
      selectedRows: [],
      formData: {
        orderId: '',
        userName: '',
        userPhone: '',
        time: []
      },
      modifydata: {
        trackingNumber: ''
      },
      tableData: [],
      currentRow: null
    }
  },
  methods: {
    // Implement pagination methods
    queryByPage() {
      const start = (this.currentPage - 1) * this.pageSize
      const end = this.currentPage * this.pageSize
      return this.tableData.slice(start, end)
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.pageData = this.queryByPage()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.pageData = this.queryByPage()
    },
    // Search button click handler
    handleSubmitClick() {
      const { orderId, userName, userPhone, time } = this.formData
      const strTime = time.length ? time[0] : ''
      const endTime = time.length ? time[1] : ''

      searchsendorderlist({ orderId, userName, userPhone, strTime, endTime })
        .then(res => {
          if (res.success) {
            this.tableData = res.data
            this.total = res.data.length
            this.pageData = this.queryByPage()
          }
        })
        .catch(err => {
          console.error('Error fetching data:', err)
        })
    },

    handleReceiveOrder() {
      this.dialogVisible = true
    },
    confirmReceiveOrder() {
      const selectedRows = this.tableData.filter(row => {
        return this.selectedRows.some(selected => selected.id === row.id)
      })
      //  console.log('选中的订单数据:', selectedRows)
      if (this.selectedRows.length === 0) {
        this.$message({
          message: '请至少选择一个订单进行退款',
          type: 'warning'
        })

        this.dialogVisible = false
        return
      } else if (this.modifydata.trackingNumber === '') {
        //  console.log('zzd')
        this.$message({
          message: '请填写快递单号',
          type: 'warning'
        })
        return
      }
      const id = this.$store.getters.allUserInfo.id
      const name = this.$store.getters.allUserInfo.name
      const trackingNumber = this.modifydata.trackingNumber
      let params = { data: selectedRows, useradmin: name, adminId: id , trackingNumber:  trackingNumber }

      allsend(params)
        .then(res => {
          if (res.success) {
            this.$message({
              message: '发货成功',
              type: 'success'
            })
            //  console.log(res)
            this.handleSubmitClick()
          }
        })
        .catch(err => {
          console.error('Error sending orders:', err)
        })

      this.dialogVisible = false
    },
    handleSelectionChange(val) {
      this.selectedRows = val
    }
  },
  mounted() {
    searchsendorderlist().then(res => {
      if (res.success) {
        this.tableData = res.data
        this.total = res.data.length
        this.pageData = this.queryByPage()
      }
    })
  }
}
</script>

<style lang="scss" scoped>
.wrapper {
  margin: 40px 0;
}
.selector-wrapper {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.list-wrapper {
  margin-top: 40px;
}
.el-form-item--small.el-form-item {
  margin-bottom: 0px;
}
.form-item-wrapper {
  display: flex;
  justify-content: space-between;
}
.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 10px;
}
.image-preview {
  max-width: 100px;
  max-height: 50px;
}
.button-wrapper {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
.dialog-style {
  display: flex;
  align-items: center;
  justify-content:flex-start;
}
</style>
